package com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.bo.request.XjsReviewLimitTechDocQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.bo.request.XjsReviewLimitTechDocSaveRequest;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.bo.response.XjsReviewLimitTechDocResponse;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.model.XjsReviewLimitTechDoc;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.dao.XjsReviewLimitTechDocDao;
import com.fufan.mtsaas.modules.newtech.xjsreviewlimittechdoc.service.XjsReviewLimitTechDocSvc;
import com.fufan.mtsaas.util.DateFormatTool;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 年度限制类人员档案表 服务实现类
 * </p>
 *
 * @author Liby
 * @since 2025-02-14
 */
@Service("XjsReviewLimitTechDocSvcImpl")
@Slf4j
@Primary
public class XjsReviewLimitTechDocSvcImpl extends ServiceImpl<XjsReviewLimitTechDocDao, XjsReviewLimitTechDoc> implements XjsReviewLimitTechDocSvc {
    @Resource
    private XjsReviewLimitTechDocDao xjsReviewLimitTechDocDao;


    @Override
    public Result doQueryPage(XjsReviewLimitTechDocQueryRequest map) {

        Result<XjsReviewLimitTechDocResponse> result = new Result<>();
        Page<XjsReviewLimitTechDocResponse> page = new Page<>();
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsReviewLimitTechDocDao.selectByPage(page, map);
            }
        });

        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        //分页查询
        List<XjsReviewLimitTechDocResponse> list = xjsReviewLimitTechDocDao.selectByPage(page, map);

        page.setRecords(list);
        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }

    @Override
    public Result doSave(XjsReviewLimitTechDocSaveRequest entity, RequestPojo requestPojo) {
        Result result = new Result();
        Page<XjsReviewLimitTechDoc> page = new Page<>();
        XjsReviewLimitTechDoc map = new XjsReviewLimitTechDoc();
        map.setName(entity.getName());
        map.setCorrelationId(entity.getCorrelationId());
        List<XjsReviewLimitTechDoc> list = xjsReviewLimitTechDocDao.queryAll(map, page);
        if (CollectionUtils.isNotEmpty(list)) {
            result.setResStatus(ResStatus.STATUS_EXISTENCE_MSG);
            result.setResMsg(ResStatus.STATUS_EXISTENCE_MSG + "， " + entity.getName() + " 人员已存在");
            return result;
        }

        int rec = xjsReviewLimitTechDocDao.insert(entity);

        result.setResData(page.setRecords(Arrays.asList(entity)));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;
    }

    @Override
    public Result doUpdate(XjsReviewLimitTechDocSaveRequest entity, RequestPojo requestPojo) {
        Result result = new Result();
        Page<XjsReviewLimitTechDoc> page = new Page<>();
        XjsReviewLimitTechDoc map = new XjsReviewLimitTechDoc();
        map.setName(entity.getName());
        map.setCorrelationId(entity.getCorrelationId());
        List<XjsReviewLimitTechDoc> list = xjsReviewLimitTechDocDao.queryAll(map, page);
        if (CollectionUtils.isNotEmpty(list) && list.get(0).getId() != entity.getId()) {
            result.setResStatus(ResStatus.STATUS_EXISTENCE_MSG);
            result.setResMsg(ResStatus.STATUS_EXISTENCE_MSG + "， " + entity.getName() + " 人员已存在");
            return result;
        }

        int rec = xjsReviewLimitTechDocDao.update(entity);

        result.setResData(page.setRecords(Arrays.asList(entity)));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;
    }

    @Override
    public Result doDelete(XjsReviewLimitTechDocQueryRequest map, RequestPojo requestPojo) {

        Result result = new Result();

        if (Objects.isNull(map.getId()) || map.getId() == 0) {
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG + " id不存在");

            return result;
        }
        int rec = xjsReviewLimitTechDocDao.deleteById(map.getId());

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;

    }

}
